package com.ruoyi.lc.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.mapper.CommonMapper;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lc.biz.domain.BizType;
import com.ruoyi.lc.biz.service.IBizTypeService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.*;

/**
 * 业务类型Controller
 *
 * @author qibotian
 * @date 2025-03-04
 */
@RestController
@RequestMapping("/lc/biz/data")
public class BizDataController extends BaseController {

    @Autowired
    private IBizTypeService bizTypeService;

    @Autowired
    private CommonMapper commonMapper;


    /**
     * 查询业务类型列表
     */
    @GetMapping("/page")
    public AjaxResult page(
            @ApiParam(value = "当前页码", required = true) @RequestParam String type,
            @ApiParam(value = "当前页码", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "流程名称", required = false) @RequestParam(required = false) String name) {
        PageHelper.startPage(pageNum, pageSize);
        String tableName = null;
        if ("WORKFLOW".equals(type)) {
            tableName = "act_re_procdef";

        } else {
            BizType bizType = bizTypeService.queryByCode(type);
            if (bizType != null) {
                tableName = bizType.getMasterTableName();
            }
        }
        List<HashMap<String, Object>> datas = commonMapper.selectDatas(tableName);
        Page<HashMap<String, Object>> page = new Page<>();
        page.setTotal(new PageInfo(datas).getTotal());
        if (datas != null) {
            dealTypeDatas(type, datas);
            List<HashMap<String, Object>> result = new ArrayList<>();
            for (HashMap<String, Object> map : datas) {
                result.add(MapUtils.convertKeysToCamelCaseInPlace(map));
            }
            datas = result;
        }
        page.setRecords(datas);
        return success(page);
    }

    /**
     * 查询业务类型列表
     */
    @GetMapping("/queryDatasByIds/{type}/{ids}")
    public AjaxResult queryDatasByIds(@PathVariable("type") String type, @PathVariable("ids") String idsStr) {
        String[] ids = idsStr.split(",");
        List<HashMap<String, Object>> datas = null;
        if ("WORKFLOW".equals(type)) {
            datas = commonMapper.selectDatasByIds("act_re_procdef", "id_", ids);
            for (HashMap<String, Object> data : datas) {
                data.put("id", data.get("ID_"));
                data.put("code", data.get("ID_"));
                data.put("title", data.get("NAME_"));
            }
        } else {
            BizType bizType = bizTypeService.queryByCode(type);
            if (bizType != null) {
                datas = commonMapper.selectDatasByIds(bizType.getMasterTableName(), "id", ids);
            }
        }
        if (datas != null) {
            List<HashMap<String, Object>> result = new ArrayList<>();
            for (HashMap<String, Object> map : datas) {
                result.add(MapUtils.convertKeysToCamelCaseInPlace(map));
            }
            return success(result);
        }
        return success(datas);
    }

    private void dealTypeDatas(String type, List<HashMap<String, Object>> datas) {
        if ("WORKFLOW".equals(type)) {
            for (HashMap<String, Object> data : datas) {
                data.put("id", data.get("ID_"));
                data.put("code", data.get("ID_"));
                data.put("title", data.get("NAME_"));
            }
        }
    }
}


